<template>
	<health-card-users type="auth" hospitalId="37868" @select="selectCallback" @addCard="addCard"
		@switchType="switchTypeCallback" />
</template>

<script>
	import healthCardApi from './healthCard';
	export default {
		data() {
			return {

			}
		},
		onLoad() {

		},
		methods: {
			// 选卡回调
			selectCallback(result) {
				console.log('selectCallback: ', result);
				const data = result.detail || {};
				const {
					healthCode = ''
				} = data;
				// 获取健康卡详情信息
				healthCardApi.api_gethealthcard(healthCode).then((res) => {
					console.log(res)
					if (res.status == 1) {
						let info = JSON.parse(res.data)
						console.log(info)
						let formData = {
							"CardNo": "",
							"CardType": "1",
							"Name": info.name,
							"Sex": info.gender == '男' ? '1' : '2',
							"BirthDay": info.birthday,
							"IDNum": info.idNumber,
							"IDType": '01',
							"PhoneNo": info.phone1,
							"Password": "",
							"EcardNo": info.ecardNo
						}
						let openid = uni.getStorageSync('openid')
						healthCardApi.api_createprofile(openid, formData).then((res) => {
							if (res.status == 1) {
								/* let userCardInfo = uni.getStorageSync('user_cardinfo');
								userCardInfo.cardName = res.data.name
								userCardInfo.cardid = res.data.id
								uni.setStorageSync('user_cardinfo', userCardInfo) */
								let openid = uni.getStorageSync('openid')
								let user_cardlist = uni.getStorageSync('user_cardlist')
								let current_user_card = uni.getStorageSync('current_user_card')
												
								this.$app._post_form("entry/wxapp/apipatient.cardlist", {
									'openid': openid
								}, (res) => {
									if (res.status === 1) {
										let userCardList = res.data
										uni.setStorageSync('user_cardlist', userCardList)
										uni.setStorageSync('current_user_card', userCardList[0])
										uni.navigateTo({
											url: '/pages/global/successMessage?msg=建档成功'
										})
									}
								})
							} else {
								uni.showToast({
									title: '建档失败',
									icon: "none"
								});
							}
						})
					}
				})
				// TODO 获取healthCode后处理业务逻辑
			},
			// 点击添加用卡人
			addCard(event) {
				console.log('addCard', event);
				// TODO 跳转到开发商建卡页面
				uni.navigateTo({
					url: '/pagesB/healthCard/create'
				})
			},
			// 点击管理健康卡or完成管理
			switchTypeCallback(data) {
				console.log('切换type到：', data.detail.type);
			},
		}
	}
</script>

<style>
</style>